package com.tyrone.captcha.controller;

import com.tyrone.captcha.config.aop.RequestLimit;
import com.tyrone.captcha.entity.constant.ResponseCode;
import com.tyrone.captcha.entity.constant.SlideImageCodeConstant;
import com.tyrone.captcha.entity.dto.SlideImageCodeCheckDto;
import com.tyrone.captcha.entity.Response;
import com.tyrone.captcha.entity.vo.ImageCodeVo;
import com.tyrone.captcha.service.SimpleImageCodeService;
import com.tyrone.captcha.service.SlideImageCodeService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.io.*;

import java.util.List;

/**
 * @author xieyl
 * @create 2021/9/10 14:06
 */
@RestController
public class CodeController {

    @Autowired
    private SlideImageCodeService slideImageCodeService;

    @Autowired
    private SimpleImageCodeService simpleImageCodeService;


    /**
     * @throws IOException
     */
    @RequestLimit()
    @GetMapping(value = "/slideImageCode/get")
    public Response getSlideImageCode() throws IOException {
        ImageCodeVo picCode = slideImageCodeService.createImageCode();
        return new Response(ResponseCode.SUCCESS, picCode, "图片验证码获取成功");
    }

    @PostMapping("/slideImageCode/check")
    public Response checkSlideImageCode(@RequestBody @Valid SlideImageCodeCheckDto slideImageCodeCheckDto) {
        if (!slideImageCodeService.trajectoryVerification(slideImageCodeCheckDto.getTrajectory())) {
            return new Response(ResponseCode.FAILURE, SlideImageCodeConstant.IMAGE_CODE_CHECK_FAILURE, "图片验证码校验失败");
        }
        if (!slideImageCodeService.checkImageCode(slideImageCodeCheckDto.getId(), slideImageCodeCheckDto.getOffset())){
            return new Response(ResponseCode.SUCCESS, SlideImageCodeConstant.IMAGE_CODE_CHECK_FAILURE,"图片验证码校验失败");
        }
        return new Response(ResponseCode.SUCCESS, SlideImageCodeConstant.IMAGE_CODE_CHECK_SUCCESS, "图片验证码校验成功");
    }

    // TODO: 2021/11/2 需要确认入参num是验证码的个数还是字母的个数
    @RequestLimit(requestNumLimit = 3)
    @GetMapping("/simpleImageCode/get")
    public Response getSimpleImageCode(@RequestParam String sessionId, @RequestParam Integer width, @RequestParam Integer height, @RequestParam Integer num,@RequestParam(defaultValue = "5") Integer size) throws IOException {
        if (sessionId == null && width == null && height == null && num == null) {
            return new Response(ResponseCode.FAILURE,"图片验证码获取失败");
        }
        List<String> simpleImageCodeList = simpleImageCodeService.createImageCode(sessionId, width, height, num);
        return new Response(ResponseCode.SUCCESS, simpleImageCodeList, "图片验证码获取成功");
    }


    /**
     * sessionId=211312312(String)   code : string   checkOnly:0清空 ,1 不清空
     *
     * @return
     */
    @GetMapping("/simpleImageCode/check")
    public Response checkSimpleImageCode(@RequestParam String sessionId, @RequestParam String code, @RequestParam(required = false) Integer checkOnly) {
        if (sessionId == null && code == null) {
            return new Response(ResponseCode.FAILURE, "图片验证码校验失败");
        }
        if(simpleImageCodeService.checkImageCode(sessionId, code, checkOnly)){
            return new Response(ResponseCode.SUCCESS, SlideImageCodeConstant.IMAGE_CODE_CHECK_SUCCESS, "图片验证码校验成功");
        }
        return new Response(ResponseCode.SUCCESS, SlideImageCodeConstant.IMAGE_CODE_CHECK_FAILURE, "图片验证码校验成功");
    }

}
